package app.gui;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import app.Main;

/**
 * Dialog Frame with Records Diagram
 * 
 * @author Lukas Vavra
 *
 */
public class RecordsDialog extends JDialog {
	
	private RecordsDiagram diagram;
	private RecordsModel model;
	
	/**
	 * Constructor
	 * 
	 * @param model Records model with loaded data
	 */
	public RecordsDialog(RecordsModel model) {
		this.setModal(false);
		this.setTitle("Okno záznamů");
		this.model = model;
		this.model.refresh();
		if(this.model.isEmpty()) {
			Main.showInfo("Žádné záznamy k zobrazení");
			dispose();
			return;
		}
		this.setLayout(new GridBagLayout());
		GridBagConstraints cs = new GridBagConstraints();
		cs.fill = GridBagConstraints.BOTH;
		cs.weightx = 1;
		cs.weighty = 1;
		
		diagram = new RecordsDiagram(model);
		cs.gridx = 0;
		cs.gridy = 0;
		cs.gridwidth = 1;
		cs.gridheight = 1;
		this.add(diagram, cs);
		
		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 10, 10));
		JButton clearBut = new JButton("Odstranit záznamy");
		clearBut.addActionListener(new ClearButtonListener());
		buttonPanel.add(clearBut);
		
		JButton refreshBut = new JButton("Obnovit");
		refreshBut.addActionListener(new RefreshButtonListener());
		buttonPanel.add(refreshBut);
		
		JButton closeBut = new JButton("Zavřít");
		closeBut.addActionListener(new CloseButtonListener());
		buttonPanel.add(closeBut);
		
		cs.gridx = 0;
		cs.gridy = 1;
		cs.gridwidth = 1;
		cs.gridheight = 1;
		cs.weighty = 0;
		this.add(buttonPanel, cs);
				
		this.setSize(new Dimension(800, 400));
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
			
	/**
	 * Clear Button Listener
	 * 
	 * @author Lukas Vavra
	 *
	 */
	private class ClearButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			model.clear();
			dispose();
		}
		
	}
	/**
	 * Close Button Listener
	 * 
	 * @author Lukas Vavra
	 *
	 */
	private class CloseButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			dispose();			
		}
		
	}
	
	/**
	 * Refresh Button Listener
	 * 
	 * @author Lukas Vavra
	 *
	 */
	private class RefreshButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			model.refresh();
			diagram.refresh();			
		}
	}
}
